//
//  CLLiveViewController.m
//  chanlin
//
//  Created by Ryan on 15/6/4.
//  Copyright (c) 2015年 chanlin. All rights reserved.
//

#import "CLLiveViewController.h"
#import "UIView+Utils.h"
#import "CLLoginInViewController.h"
#import "CLChatViewController.h"
#import "BlockUI.h"
#import "CLPushAnimatedRight.h"
#import "ChangyanSDK.h"
#import "CLComentDataModel.h"
#import "CLLiveHostView.h"
#import "CLLiveDataModel.h"
#import "UIImageView+WebCache.h"
#import "UIView+Utils.h"


@interface CLLiveViewController ()

@property (nonatomic,strong) UIImageView *headImg;
@property (nonatomic,strong) UILabel *titleLabel;
@property (nonatomic,strong) NSString *headUrl;

@property (nonatomic,strong) NSString *titleStr;
@property (nonatomic,strong) NSDictionary *dic;

@property (nonatomic,strong) UIView *menu;
@property (nonatomic,strong) UIView *line;

@property (nonatomic,strong) UIButton *left;
@property (nonatomic,strong) UIButton *right;
@property (nonatomic,strong) UIScrollView *contentScroll;

@property (nonatomic,strong) CLLiveDataModel *dataModel;

@property (nonatomic,strong)NSString *shareUrl;
@property (nonatomic,strong)NSString *thumnail;






@property (nonatomic,weak) CLLiveHostView *hostView;


@end

@implementation CLLiveViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
   self.view.backgroundColor=RGB(0xf8, 0xf8, 0xf8);
    //self.itemId=@"5453";
    self.dataModel=[CLLiveDataModel new];
    __weak typeof(self) me=self;
    [self.dataModel getMessage:self.itemId:^(BOOL success, id responseObj, NSError *message, NSMutableDictionary *param) {
        if(success){
            NSDictionary *data=[responseObj sgrGetDictionaryForKey:@"data"];
            me.titleStr=[data sgrGetStringForKey:@"title"];
            me.headUrl=[data sgrGetStringForKey:@"bspic"];
            me.shareUrl=[data sgrGetStringForKey:@"shareurl"];
            me.thumnail=[data sgrGetStringForKey:@"sharepic"];
        }
        [me initView];
    }];
    
    
    
}

- (void)initView{
    self.headImg=[[UIImageView alloc]
                  initWithFrame:CGRectMake(0, 20, GlobleWidth, 96.f)];
    if(_isStrNotNull(self.headUrl))
    [self.headImg sd_setImageWithURL:[NSURL URLWithString:self.headUrl]];
    
    NSString *title=self.titleStr;
    
    
    CGSize size=[title sizeWithFont:[UIFont boldSystemFontOfSize:17.f]];
    self.titleLabel=[[UILabel alloc] init];
    self.titleLabel.textAlignment=NSTextAlignmentCenter;
    self.titleLabel.font=[UIFont boldSystemFontOfSize:17.f];
    self.titleLabel.frame=CGRectMake(10.f, self.headImg.bottom, 300.f, self.titleLabel.font.lineHeight);
    self.titleLabel.textColor=RGB(67, 70, 78);
    self.titleLabel.text=title;
    self.titleLabel.numberOfLines=2;
    
    
    
    if(size.width>300){
        self.titleLabel.height=2*self.titleLabel.font.lineHeight;
    }
    
    self.titleLabel.height+=30.f;
    
    [self.view addSubview:self.headImg];
    [self.view addSubview:self.titleLabel];
    
    self.menu=[[UIView alloc] initWithFrame:CGRectMake(0, self.titleLabel.bottom, GlobleWidth, 35.f)];
    [self.view addSubview:self.menu];
    
    UIButton *left=[UIButton buttonWithType:UIButtonTypeCustom];
    left.frame=CGRectMake(0, 0, 160.f, 35.f);
    [left setBackgroundColor:RGB(229, 230, 233)];
    left.titleLabel.font=[UIFont systemFontOfSize:12.f];
    [left setTitle:@"直播厅" forState:UIControlStateNormal];
    [left setTitleColor:RGB(97, 100, 112) forState:UIControlStateNormal];
    [left setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
    [self.menu addSubview:left];
    self.left=left;
    [self.left addTarget:self action:@selector(clickButtpn:) forControlEvents:UIControlEventTouchUpInside];
    
    UIButton *right=[UIButton buttonWithType:UIButtonTypeCustom];
    right.frame=CGRectMake(left.right, 0, 160.f, 35.f);
    [right setBackgroundColor:RGB(229, 230, 233)];
    [right setTitle:@"大家在说" forState:UIControlStateNormal];
    right.titleLabel.font=[UIFont systemFontOfSize:12.f];
    [right setTitleColor:RGB(97, 100, 112) forState:UIControlStateNormal];
    [right setTitleColor:[UIColor redColor] forState:UIControlStateSelected];
    [self.menu addSubview:right];
    self.right=right;
    [self.right addTarget:self action:@selector(clickButtpn:) forControlEvents:UIControlEventTouchUpInside];
    
    self.line=[[UIView alloc] init];
    self.line.frame=CGRectMake(0, 34.f, 160.f, 1);
    self.line.backgroundColor=[UIColor redColor];
    [self.menu addSubview:self.line];
    self.left.selected=YES;
    
    self.contentScroll=[[UIScrollView alloc] initWithFrame:CGRectMake(0, self.menu.bottom, GlobleWidth, GlobleHeight-self.menu.bottom)];
    [self.view addSubview:self.contentScroll];
    self.contentScroll.contentSize=CGSizeMake((2*GlobleWidth), self.contentScroll.height);
    self.contentScroll.bounces=YES;
    self.contentScroll.pagingEnabled=YES;
    self.contentScroll.delegate=self;
    
    
    CLChatViewController *chat1=[CLChatViewController new];
    [chat1 setRoom:1];
    chat1.titlestr=self.title;
    chat1.liveId=[NSString stringWithFormat:@"%@_live",self.itemId];
    //chat1.view.frame=CGRectMake(0, 0, GlobleWidth, self.contentScroll.height);
    [chat1 setFrame:CGRectMake(0, 0, GlobleWidth, self.contentScroll.height)];
    [self addChildViewController:chat1];
   [self.contentScroll addSubview:chat1.view];
   self.chat1=chat1;
    self.chat1.parent=self;
    
    
    CLChatViewController *chat2=[CLChatViewController new];
    chat2.titlestr=self.title;
    chat2.room=2;
    chat2.liveId=[NSString stringWithFormat:@"%@_chat",self.itemId];
    //chat2.view.frame=CGRectMake(GlobleWidth, 0, GlobleWidth, self.contentScroll.height);
    [chat2 setFrame:CGRectMake(GlobleWidth, 0, GlobleWidth, self.contentScroll.height)];
    [self addChildViewController:chat2];
    [self.contentScroll addSubview:chat2.view];
    self.chat2=chat2;
    self.chat2.parent=self;

    
    self.tool=[self showToolbarWithType:IFActive_ButtomToolBarButtonTypeShare];
    
    __weak typeof(self) me=self;
    [self.tool handleTap:IFActive_ButtomToolBarButtonTypeShare block:^(id sender) {
        
        
        NSMutableDictionary *dic=[NSMutableDictionary dictionary];
        //        [dic sgrSetObject:[UIImage imageNamed:@"icon114.png"] forKey:@"icon"];
        
        [dic sgrSetObject:[me getImageFromURL:me.thumnail] forKey:@"icon"];
        
        [dic sgrSetObject:me.titleStr forKey:@"title"];
        
        
        [dic sgrSetObject:me.shareUrl  forKey:@"url"];
        [me openShare:dic];
    }];
    
    self.tool.view.backgroundColor=RGB(239, 240, 241);
    
    UIView *bg=[[UIView alloc] init];
    bg.backgroundColor=RGB(251, 253, 252);
    bg.frame=CGRectMake(43.f, (self.tool.view.height-30.f)/2.f, GlobleWidth-43.f-13.f-50, 30.f);
    [self.tool.view addSubview:bg];
    
    UIImageView *wi=[[UIImageView alloc] initWithFrame:CGRectMake(8, 3, 24.f, 24.f)];
    wi.image=[UIImage imageNamed:@"tb_wComment.png"];
    wi.userInteractionEnabled=YES;
    [bg addSubview:wi];
    
    UILabel *commentl=[[UILabel alloc] initWithFrame:CGRectMake(wi.right+10, 0, 150, 30.f)];
    commentl.font=[UIFont systemFontOfSize:12];
    commentl.textColor=RGB(160, 160, 160);
    commentl.text=@"我来说两句";
    [bg  addSubview:commentl];
    
    UIButton *cmtbutton=[UIButton buttonWithType:UIButtonTypeCustom];
    
    cmtbutton.frame=bg.bounds;
    [bg addSubview:cmtbutton];
    
    [cmtbutton handleControlEvent:UIControlEventTouchUpInside withBlock:^(id sender) {
        //if([@"0" isEqualToString:[CLGloble sharedInstance].user.groupId]){
        
        if(![[CLGloble sharedInstance] isLogin]){
            
            CLLoginInViewController *login=[[CLLoginInViewController alloc] init];
            [[CLPushAnimatedRight sharedInstance] pushController:login];
            return ;
        }
        
        
        
       
        
        if([@"8" isEqualToString:[CLGloble sharedInstance].user.groupId]){
            [me openHostView];
        }else{
            [me openComment:chat2.liveId comfirmPress:^(NSString *content, int select) {
                
                
                
                if(_isStrNULL(content) ){
                    [[CLGloble sharedInstance] showTieAlertMessage:@"评论数据不能为空" subMessage:@"请修改"];
                    return ;
                }
                
//                if([content length]>300){
//                    [[CLGloble sharedInstance] showTieAlertMessage:@"评论不能超过300个字" subMessage:@"请修改"];
//                    return;
//                }
                
                [[CLComentDataModel sharedInstance] submitComment:chat2.topicId
                                                          content:content
                                                          replyID:nil
                                                            block:^(BOOL success, NSString *commentId) {
                                                                if (success) {
                                                                    [[CLGloble sharedInstance] showTieSuccessMessage:@"提示" subMessage:@"发送成功"];
                                                                    [me.inputView resign];
                                                                    
                                                                }else{
                                                                    [[CLGloble sharedInstance] showTieAlertMessage:@"提示" subMessage:@"发送失败"];
                                                                }
                                                                me.isInputShow=NO;
                                                            }];
                
                
            }];
        }
      
        
        
    }];

    
}

-(UIImage *)getImageFromURL:(NSString *)fileURL {
    UIImage * result;
    NSData * data = [NSData dataWithContentsOfURL:[NSURL URLWithString:fileURL]];
    result = [UIImage imageWithData:data];
    if (result) {
        return result;
    }else
        return [UIImage imageNamed:@"icon114.png"];
}

- (void)clickButtpn:(id)sender{
    if(sender==self.left){
        [self.contentScroll scrollRectToVisible:CGRectMake(0, 0, GlobleWidth, self.contentScroll.height) animated:YES];
        self.right.selected=NO;
        self.left.selected=YES;
    }else if(sender==self.right){
        [self.contentScroll scrollRectToVisible:CGRectMake(GlobleWidth, 0, GlobleWidth, self.contentScroll.height) animated:YES];
        self.right.selected=YES;
        self.left.selected=NO;

        
    }
}


- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    float x=scrollView.contentOffset.x/2.f;
    self.line.frame=CGRectMake(x, 34.f, 160.f, 1);
    
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate{
    if(decelerate){
        
    }else{
        if(scrollView.contentOffset.x>=GlobleWidth){
            self.line.frame=CGRectMake(160.f, 34.f, 160.f, 1);
            self.left.selected=NO;
            self.right.selected=YES;
        }else{
            self.line.frame=CGRectMake(0, 34.f, 160.f, 1);
            self.left.selected=YES;
            self.right.selected=NO;
        }
    }
}

- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView{
    if(scrollView.contentOffset.x>=GlobleWidth){
        self.line.frame=CGRectMake(160.f, 34.f, 160.f, 1);
        self.left.selected=NO;
        self.right.selected=YES;
    }else{
        self.line.frame=CGRectMake(0, 34.f, 160.f, 1);
        self.left.selected=YES;
        self.right.selected=NO;
    }


}


- (void)replay:(NSString *)str{
    
    NSString *topicId=[@"8" isEqualToString:[CLGloble sharedInstance].user.groupId]?self.chat1.topicId:self.chat2.topicId;
    __weak typeof(self) me=self;
    [[[CLGloble sharedInstance] currentController]
     openComment:topicId replayId:nil backString:str block:^(BOOL cont, NSString *select) {
         if([@"8" isEqualToString:[CLGloble sharedInstance].user.groupId]){
             [me.chat1 checkReflahs];
         }else{
             [me.chat2 checkReflahs];
         }
    
     }];

}


- (void)openHostView{
    
    CLLiveHostView *host=[[CLLiveHostView alloc] init];
    host.ctrl=self;
    host.frame=self.view.bounds;
    [self.view addSubview:host];
    [self.view bringSubviewToFront:host];
    self.hostView=host;
}



@end
